package com.review.dp;

public class LC_122 {
    public static int maxProfit(int[] prices) {
        // 寻找局部最优
        int sum = 0, min = prices[0];
        for (int i = 1; i < prices.length; i++) {
            while (i < prices.length && prices[i] > min && prices[i] > prices[i - 1]) {
                i++;
            }
            if (prices[i - 1] > min) {
                sum += prices[i - 1] - min;
            }
            if (i < prices.length) {
                min = prices[i];
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7,6,4,3,1}));
    }
}
